package com.aiju.cash.ui.setting;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.util.CheckVersion;
import com.aiju.utils.T;

/**
 * @author: 佐为
 * @date: 2016-12-19 17:52
 * @version: V1.0
 * @Description: 关于我们
 */
public class AboutActivity extends BaseTitleActivity implements View.OnClickListener {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_webview);
        setCenterTitle("关于我们");

        String URL = "https://store.ecbao.cn/aboutus/ui?token=" + mUser.getToken();
        webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl(URL);

        WebSettings webViewSettings = webView.getSettings();
        webViewSettings.setJavaScriptEnabled(true);
        webViewSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webViewSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        webViewSettings.setSupportZoom(true); // 缩放至屏幕的大小
        webViewSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webViewSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        webView.setWebViewClient(new HelloWebViewClient());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_about_wechat:
                sendWechat();
                break;
            case R.id.rl_about_http:
                sendHttp();
                break;
            case R.id.rl_about_version:
                checkVersion();
                break;
            case R.id.rl_about_phone:
                callPhone();
                break;
            case R.id.iv_qq:
                qq();
                break;
        }
    }

    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //Web视图
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("http:") || url.startsWith("https:")) {
                view.loadUrl(url);
                return false;
            } else {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();

            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }

    /**
     * qq
     */
    private void qq() {
        final String qqUrl = "mqqwpa://im/chat?chat_type=wpa&uin=100000&version=1";
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqUrl)));
    }

    /**
     * 客服电话
     */
    private void callPhone() {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + "0571-89935939");
        intent.setData(data);
        startActivity(intent);
    }

    /**
     * 检查版本
     */
    private void checkVersion() {
        new CheckVersion(this, true).updateApp();
    }

    /**
     * 官网
     */
    private void sendHttp() {
        Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("pay.ecbao.cn"));
        it.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
        startActivity(it);
    }

    /**
     * 微信公众号
     */
    private void sendWechat() {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText("ajsyjz");
        T.ToastShort("公众号已复制");
    }


}
